/**
 * @file fan.c
 * @author zhengchunyu (ait_zhengchunyu@163.com)
 * @brief 风扇相关操作
 * @version 0.1
 * @date 2022-02-05
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#include "fan.h"
#include "../dbg/dbg.h"

struct fan_dev_t fan_device;

static uint8_t fan_run= 1;
/**
 * @brief fan 定时器开始计数
 * 
 * @param dev 
 */
void fan_start_timer(struct fan_dev_t *dev)
{
    dev->count = get_clock_val();
    dev->count +=60;
}
/**
 * @brief  fan 计数器停止计数
 * 
 * @param dev 
 */
void fan_stop_timer(struct fan_dev_t *dev)
{
    dev->count =0;
}
/**
 * @brief fan计数器等待
 * 
 * @param dev 
 * @return uint8_t  1 时间到
 */
uint8_t fan_wait_timer(struct fan_dev_t *dev)
{
    if(dev->count>get_clock_val())
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

void fan_set_speed(struct fan_dev_t *dev,uint8_t value) 
{
    if(value>=100)value=100;
    if(value<=0)value=0;
    dev->speed = value;
		if(fan_run==0)
		{
			    dev->hal_set_speed(0);
		}
		else{
			    dev->hal_set_speed(value);
		}

}
uint8_t fan_get_speed(void)
{
    return fan_device.speed;
}
void fan_disable(void)
{
	fan_run = 0;
}
void fan_enable(void)
{
	fan_run = 1;
}
void fan_init(struct fan_dev_t **dev)
{
	*dev = &fan_device;
    utimer0_pwm_init();
    fan_device.speed  = 20;
    fan_device.state = FAN_STABEL;
    fan_device.hal_set_speed=  utimer0_set_pwm;
    fan_device.set_speed    =   fan_set_speed;
    fan_device.get_speed    =   fan_get_speed;
    fan_device.start_wait   =   fan_start_timer;
    fan_device.stop_wait    =   fan_stop_timer;
    fan_device.waitting     =   fan_wait_timer;
		fan_device.disable = fan_disable;
		fan_device.enable  = fan_enable;
    fan_device.set_speed(&fan_device,20);

}


